#include "HalObjects.h"

bool HalObjects::instanceFlag = false;
HalObjects* HalObjects::single = NULL;
HALinput* HalObjects::halInput = NULL;
HALoutput* HalObjects::halOutput = NULL;

HalObjects* HalObjects::getInstance()
{
  if(!instanceFlag)
  {
    HalObjects::setVars();
    return single;
  }
  else
  {
    return single;
  }
}

HALinput* HalObjects::getHalInput()
{
  if(!instanceFlag)
  {
    HalObjects::setVars();
    return HalObjects::halInput;
  }
  else
  {
    return HalObjects::halInput;
  }
}

HALoutput* HalObjects::getHalOutput()
{
  if(!instanceFlag)
  {
    HalObjects::setVars();
    return HalObjects::halOutput;
  }
  else
  {
    return HalObjects::halOutput;
  }
}

//nog niet netjes, CS init moet naar HAL
void HalObjects::setVars()
{
  HalObjects::single = new HalObjects();
  Centipede CS;
  HalObjects::halInput = new HALinput(CS);
  HalObjects::halOutput = new HALoutput(CS);
  instanceFlag = true;
  
  CS.initialize(); // set all registers to default
  
  for (int i = 0; i <= 15; i++) 
  {
    CS.pinMode(i, OUTPUT);
  }
  
  CS.digitalWrite(OUT_GROUP2, LOW);
  CS.digitalWrite(OUT_GROUP1, LOW);
  CS.digitalWrite(OUT_STROBE, LOW);
  CS.digitalWrite(OUT_KEYSELECT, HIGH);
  CS.digitalWrite(OUT_BUZZER, HIGH);
  CS.digitalWrite(OUT_HEATER, HIGH);
  CS.digitalWrite(OUT_SPEED2, HIGH);
  CS.digitalWrite(OUT_SPEED1, HIGH);
  CS.digitalWrite(OUT_DATAC, LOW);
  CS.digitalWrite(OUT_DATAB, LOW);
  CS.digitalWrite(OUT_DATAA, LOW);
  CS.digitalWrite(OUT_MOTOR_RL, LOW);
  CS.digitalWrite(OUT_SOAP1, LOW);
  CS.digitalWrite(OUT_SINK, LOW);
  CS.digitalWrite(OUT_DRAIN, LOW);
  CS.digitalWrite(OUT_LOCK, LOW);
  
  for (int i = 16; i <= 23; i++) 
  {
    CS.pinMode(i, INPUT);
    CS.pinPullup(i, HIGH);
  }
}


